package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IPolicyPermissionsDAO;
import org.jsola.hr.dto.PolicyPermissionsAddDTO;
import org.jsola.hr.dto.PolicyPermissionsUpdateDTO;
import org.jsola.hr.entity.PolicyPermissionsDO;
import org.jsola.hr.query.PolicyPermissionsQuery;
import org.jsola.hr.service.IPolicyPermissionsService;
import org.jsola.hr.vo.PolicyPermissionsListVO;
import org.jsola.hr.vo.PolicyPermissionsVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 政策速递权限
 *
 * @author zhr
 */
@Service("hrPolicyPermissionsServiceImpl")
public class PolicyPermissionsServiceImpl implements IPolicyPermissionsService {

    @Autowired
    private IPolicyPermissionsDAO policyPermissionsDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public PolicyPermissionsVO save(PolicyPermissionsAddDTO policyPermissionsAddDTO, TokenUser tokenUser) {
        // 转DO
        PolicyPermissionsDO policyPermissionsDO = policyPermissionsAddDTO.to(PolicyPermissionsDO.class);
        // 保存
        policyPermissionsDO = save(policyPermissionsDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return policyPermissionsDO.to(PolicyPermissionsVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(PolicyPermissionsUpdateDTO policyPermissionsUpdateDTO, TokenUser tokenUser) {
        //转DO
        PolicyPermissionsDO policyPermissionsDO = policyPermissionsUpdateDTO.to(PolicyPermissionsDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(policyPermissionsDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... policyPermissionsIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) policyPermissionsIds);
    }


    @Override
    public PolicyPermissionsVO selectById(String policyPermissionsId, String siteId) {
        PolicyPermissionsDO policyPermissionsDO = selectDOById(policyPermissionsId, siteId);
        if (policyPermissionsDO == null) {
            return null;
        }
        return policyPermissionsDO.to(PolicyPermissionsVO.class);
    }

    @Override
    public List<PolicyPermissionsListVO> select(PolicyPermissionsQuery policyPermissionsQuery, String siteId) {
        List<PolicyPermissionsDO> policyPermissionsDOList = selectDO(policyPermissionsQuery, siteId);
        if (CollectionUtils.isEmpty(policyPermissionsDOList)) {
            return policyPermissionsDOList == null ? null : new ArrayList<>();
        }
        return policyPermissionsDOList.stream()
                .map(policyPermissionsDO -> policyPermissionsDO.to(PolicyPermissionsListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(PolicyPermissionsQuery policyPermissionsQuery, String siteId) {
        Example example = buildExample(policyPermissionsQuery, siteId);
        return policyPermissionsDAO.selectCountByExample(example);
    }

    @Override
    public Page<PolicyPermissionsListVO> selectPage(PolicyPermissionsQuery policyPermissionsQuery, String siteId) {
        Example example = buildExample(policyPermissionsQuery, siteId);
        Page<PolicyPermissionsDO> page = policyPermissionsDAO.selectPageByExample(example,
                policyPermissionsQuery.getPageNo(),
                policyPermissionsQuery.getPageSize());

        return page.to(PolicyPermissionsListVO.class);
    }

    @Override
    public PolicyPermissionsDO selectDOById(String policyPermissionsId, String siteId) {
        return listById(policyPermissionsId, siteId);
    }

    @Override
    public List<PolicyPermissionsDO> selectDO(PolicyPermissionsQuery policyPermissionsQuery, String siteId) {
        Example example = buildExample(policyPermissionsQuery, siteId);
        return policyPermissionsDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void bachSave(List<PolicyPermissionsDO> saveList) {
        if(!CollectionUtils.isEmpty(saveList)){
            policyPermissionsDAO.insertListAndSetId(saveList);
        }
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Integer deleteByPolicyId(String policyId, String siteId) {
        if(StringUtils.isEmpty(policyId)){
            return 0;
        }
        Example example = new Example(PolicyPermissionsDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andEqualTo("policyId", policyId);
        return policyPermissionsDAO.deleteByExample(example);
    }

    @Override
    public List<PolicyPermissionsDO> selectByPolicyIdList(List<String> policyIdList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(policyIdList)){
            return new ArrayList<>();
        }
        Example example = new Example(PolicyPermissionsDO.class);
        example.and()
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andIn("policyId", policyIdList);
        return policyPermissionsDAO.selectByExample(example);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param policyPermissionsQuery 查询参数
     * @param siteId                 所属站点id
     * @return example
     */
    private Example buildExample(PolicyPermissionsQuery policyPermissionsQuery, String siteId) {
        Example example = new Example(PolicyPermissionsDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, policyPermissionsQuery.getOrders());
        return example;
    }
}




